package server.business.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import server.business.controller.dto.BagDesory;
import server.business.mapper.BloodBagDestoryMapper;
import server.business.pojo.BloodBagDestory;
import server.business.service.BloodBagDestoryService;
import server.business.service.BloodBagRecycleService;
import server.business.service.BloodInformationService;
import server.business.utils.GenerateNumberBySnowFlake;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @author wwjh
 * @date 2022年08月05日 10:57
 */
@Slf4j
@RestController
@RequestMapping("/destory")

public class BloodBagDestoryController {
    @Autowired
    private BloodBagRecycleService bagRecycleService;
    @Autowired
    private BloodBagDestoryMapper bloodBagDestoryMapper;
    @Autowired
    private BloodBagDestoryService bloodBagDestoryService;
    @Autowired
    private BloodInformationService bloodInformationService;

    @PostMapping("/normal/insert")
    public boolean destoryinsert(@RequestBody BagDesory bagDesory){
        if (StrUtil.isBlank(bagDesory.getBlood_recycle_id())||StrUtil.isBlank(bagDesory.getBlood_bag_number()) || StrUtil.isBlank(bagDesory.getDestory_people())||StrUtil.isBlank(bagDesory.getDestory_way())||StrUtil.isBlank(bagDesory.getDestory_people_number())){
            return false;
        }else{
            BloodBagDestory bloodBagDestory = new BloodBagDestory();
            String Destory_People_Number = bagDesory.getDestory_people_number();
            GenerateNumberBySnowFlake blood_Destory_id = new GenerateNumberBySnowFlake(Destory_People_Number,"XH");
            bloodBagDestory.setBloodDestoryId(blood_Destory_id.genNumberByflag());
            bloodBagDestory.setBloodBagNumber(bagDesory.getBlood_bag_number());
            bloodBagDestory.setDestoryWay(bagDesory.getDestory_way());
            bloodBagDestory.setDestoryPeople(bagDesory.getDestory_people());
            bloodBagDestory.setDestoryPeopleNumber(bagDesory.getDestory_people_number());
            bloodBagDestory.setDestoryDate(new Date());
            bloodBagDestory.setIsDeleted(0);
            bagRecycleService.setconditionbybloodbagnum(bagDesory.getBlood_bag_number());
            bloodInformationService.setStateByBloodNumber(bagDesory.getBlood_bag_number(),"16");
            bloodBagDestoryService.saveOrUpdate(bloodBagDestory);
            log.info(String.valueOf(bloodBagDestory));
            return true;
        }

    }
    @GetMapping("/normal/queryload")
    @ResponseBody
    public IPage<BloodBagDestory> findPage(HttpServletRequest request,HttpServletResponse response){
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String blood_destory_id= request.getParameter("blood_destory_id");
        String startTime = request.getParameter("startTime");
        String endTime = request.getParameter("endTime");
        String destoryPeople = request.getParameter("destoryPeople");
        String bloodBagNumber = request.getParameter("bloodBagNumber");
        String destoryWay = request.getParameter("destoryWay");
        IPage<BloodBagDestory> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BloodBagDestory> queryWrapper = new QueryWrapper<>();
        //如果销毁编号被填写
        if (blood_destory_id != "" && blood_destory_id!= null) {
            queryWrapper.like("blood_destory_id",blood_destory_id);
        }
        if (destoryPeople != "" && destoryPeople!=null){
            queryWrapper.eq("destory_people",destoryPeople);
        }
        if(bloodBagNumber!=""&& bloodBagNumber!=null){
            queryWrapper.eq("blood_bag_number",bloodBagNumber);
        }
        if(destoryWay!=""&& destoryWay!=null){
            queryWrapper.eq("destory_way",destoryWay);
        }
        if (startTime !=""&& startTime!=null){
            queryWrapper.ge("destory_date",startTime);
        }
        if (endTime != ""&& endTime!=null){
            queryWrapper.le("destory_date",endTime);
        }
        return bloodBagDestoryService.page(page,queryWrapper);

    }
    @GetMapping("/normal/query")
    @ResponseBody
    public IPage<BloodBagDestory> findPage1(HttpServletRequest request, HttpServletResponse response){
        Integer pageNum = Integer.parseInt(request.getParameter("pageNum"));
        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        IPage<BloodBagDestory> page = new Page<>(pageNum,pageSize);
        QueryWrapper<BloodBagDestory> queryWrapper = new QueryWrapper<>();
        //总查询
        return bloodBagDestoryService.page(page,queryWrapper);
    }

}
